/**
 * @author caojj
 * @version 1.1.1.0
 * 
 * @history
 * 		1.0.1.0		+ 防止重复关闭或打开的判断 @20080412
 * 		1.0.2.1		+ 淡入透明度 @20080412
 * 		1.0.3.0		# 淡入透明度设置误差 @20080414
 * 		1.0.3.3		# 更改了IE6 中 宽度 和 高度 的bug @20080504
 * 		1.0.4.5		+ 加入msgBox 与 divMask 分离的模式
 * 		1.0.4.9		+ msgBox自动居中 @20080603
 * 					# 第一次调用show()函数，计算高度可能有误的问题 @20080603
 * 		1.0.4.12	# 页面较大时高度计算问题 @20080609
 * 					# msgBox 改为屏幕居中 @20080609
 * 		1.0.4.15	# IE6中自动添加IFRAME遮挡select控件
 * 		1.0.5.1		+ 不进行渐变直接打开或关闭遮罩 @20080703
 * 		1.0.6.0		+ 用例和详细注解 @20080708
 * 		1.1.0.0		# 内容框msgBox定位问题
 * 					+ 偏移量设置 {x:0,y:0}
 * 					+ 设置滚动条模式 setScrollType [ hide|scroll|auto ]
 * 					+ 获取dom节点 getMask getBox
 * 		1.1.1.0		# 用fixed样式改进scroll的机制
 * 
 * @useage
 * 		var dom = document.getElementById("msgbox");
 * 		var pageMask = new FWK.UI.PageMask("pgMask", dom, 60);
 * 		pageMask.getMask().style.backgroundColor = "";
 * 		pageMask.setScrollType("hide");
 * 		pageMask.show();
 */

/*
 * 定义命名空间
 */
try {
	if(!FWK.UI){
		throw "ns";
	}
} catch (ex) {
	try{FWK}catch(e){FWK={}}
	FWK.UI = {}
}

var lth6 = navigator.appVersion.search(/MSIE\s+6/)>-1 ? true :false;
/**
 * 页遮罩类
 * @param {String} id			标识
 * @param {Dom} msgBox			Dom引用
 * @param {Int} aimOpacity 	透明度 
 * @param {Object} offset		偏移量{x:0,y:0}
 * @param {Enum} scrollType	滚动类型 [hide|scroll|auto]; 
 * @param {Boolean} comFlag	启用非分离模式（msgBox与pageMask分离，在透明度较低时建议设为false，默认为false）
 */
FWK.UI.PageMask = function(id, msgBox, aimOpacity, offset, scrollType, comFlag) {
	this.id = id;
	this.sepFlag = !comFlag;	
	this.V = {};
	this.V.msgBox = msgBox;
//	var divMask = document.createElement("iframe");
		
	this.aimOpacity = aimOpacity?parseInt(aimOpacity):100;
	this.init();
	
	//防止重复关闭或打开
	this.onshowing = false;
	this.onhiding  = false;
	
	this.offset = (offset&&typeof(offset)=="object") ? offset : {x:0,y:0};
//	this.scrollType = "auto";
	if (scrollType) {
		this.setScrollType(scrollType);
	}
}

/*
 * 初始化
 */
FWK.UI.PageMask.prototype.init = function() {
	var me = this;
	var divMask = document.createElement("div");
	//divMask.style.width = document.documentElement.offsetWidth + "px";
	divMask.style.width = document.body.clientWidth + "px";
	//divMask.style.height = (window.innerHeight || document.documentElement.offsetHeight) + "px";
	divMask.style.height = document.body.clientHeight + "px";
	divMask.style.zIndex = "99999";
	divMask.style.position = "absolute";
	divMask.style.top = divMask.style.left = "0";
	divMask.style.display = "none";
	divMask.style.padding = "0";
	divMask.style.margin = "0";
	divMask.style.backgroundColor = "gray";
	divMask.style.filter = "alpha(opacity=1)";
	divMask.style.opacity = "0"
	divMask.style.overflow = "hidden";
	
	this.V.mask = divMask;
	
	if(!this.V.msgBox) {
		this.V.msgBox = document.createElement("div");
	}
	

	
	this.V.msgBox.style.zIndex = "100010";
	this.V.msgBox.style.display = "none";
	this.V.msgBox.style.position = "absolute";
	
	document.body.appendChild(divMask);
	if(this.sepFlag) {
		document.body.appendChild(this.V.msgBox);		
	} else {
		divMask.appendChild(this.V.msgBox);
	}
	
	if(lth6) {
		var ifMask = document.createElement("iframe");
		ifMask.style.width = "100%";
		ifMask.style.height = divMask.style.height;
		ifMask.style.filter = "alpha(opacity=0)";
		ifMask.style.opacity = "0";	
		divMask.appendChild(ifMask);
	}
	
	function resize() {
		me.resize();
		me.fitMsgBoxPos();
	}
	
	try {		
		window.attachEvent("onresize", resize);
	} 
	catch (ex) {
		window.addEventListener("resize", resize, false);
	}
	
	this.dom = divMask;
}

/**
 * 淡入显示蒙版
 */
FWK.UI.PageMask.prototype.show = function(offset) {
	this.setOffset(offset);
	
	var me = this;
	this.showScroll();
	
	var divMask = this.V.mask;
	divMask.style.display = "";
	this.resize();
	
//	if(this.sepFlag) {		
		setTimeout(function() {
			me.getBox().style.display = "";
			me.fitMsgBoxPos();
		},200)
//		this.fitMsgBoxPos();		
//	}
	
	if(this.onshowing) {return}
	
	var i = 0;
	var sTimer = setInterval(function(){
		if (i>me.aimOpacity) {
			me.onshowing = false;
			clearInterval(sTimer);
		} else {
			me.onshowing = true;
			divMask.style.filter = "alpha(opacity="+i+")";
			divMask.style.opacity = i/100;
			i+=10;
		}
	},50)
}

/**
 * 淡出隐藏蒙版
 */
FWK.UI.PageMask.prototype.hide = function() {
	var me = this;
	this.hideScroll();
	
	var divMask = this.V.mask;
//	if(this.sepFlag) {
		this.V.msgBox.style.display = "none";		
//	}
	
	if(divMask.style.display=="none") return;
	if(this.onhiding) {return}
		
	var i = this.aimOpacity;
	var sTimer = setInterval(function(){
		if (i<0) {
			divMask.style.display = "none";
			me.onhiding = false;
			clearInterval(sTimer);
		} else {
			me.onhiding = true;
			divMask.style.filter = "alpha(opacity="+i+")";
			divMask.style.opacity = i/100;
			i-=10;
		}
	},50)
}

/**
 * 显示蒙版
 */
FWK.UI.PageMask.prototype.open = function(offset) {
	this.setOffset(offset);
	
	var me = this;
	this.showScroll();
	
	var divMask = this.V.mask;
	divMask.style.display = "";
	this.resize();
	divMask.style.filter = "alpha(opacity="+this.aimOpacity+")";
	divMask.style.opacity = this.aimOpacity/100;
	
//	if(this.sepFlag) {		
		setTimeout(function() {
			me.getBox().style.display = "";
			me.fitMsgBoxPos();
		},10)
//	}
}

/**
 * 隐藏蒙版
 */
FWK.UI.PageMask.prototype.close = function() {
	this.hideScroll();
	
	var divMask = this.V.mask;
	divMask.style.display = "none";
	divMask.style.filter = "alpha(opacity="+0+")";
	divMask.style.opacity = 0/100;
}

/**
 * 设置msgBox的偏移量
 * @param {Object} offset	{x:0,y:0}
 */
FWK.UI.PageMask.prototype.setOffset = function(offset) {
	if (offset) {
		this.offset = offset;
	}	
}

/*
 * 重新调整蒙版大小
 */
FWK.UI.PageMask.prototype.resize = function() {
	var divMask = this.V.mask;
	var dh = (window.innerHeight || document.documentElement.offsetHeight);	// + "px";
	var sh = document.documentElement.scrollHeight;
	var h = sh>dh?sh:dh;
	divMask.style.width = document.documentElement.offsetWidth + "px";
	divMask.style.height = h+"px";
	
//	this.fitMsgBoxPos();	//此时计算位置会有问题
}

/*
 * 重新调整蒙版内嵌Dom位置
 */
FWK.UI.PageMask.prototype.fitMsgBoxPos = function() {
	var divMask = this.V.mask;
	var dh = (window.innerHeight || document.documentElement.offsetHeight);	// + "px";
	var dw = divMask.offsetWidth || document.documentElement.offsetWidth;
	var st = document.documentElement.scrollTop;	
	this.V.msgBox.style.left = (dw - this.V.msgBox.offsetWidth)/2 + Number(this.offset.x) + "px";
	this.V.msgBox.style.top  = (dh - this.V.msgBox.offsetHeight)/2 + (lth6||this.scrollType!="scroll"?st:0) + Number(this.offset.y) + "px";	
}

/**
 * 设置页面滚动条呈现方式
 * @param {Object} type	[auto|hide|scroll]
 */
FWK.UI.PageMask.prototype.setScrollType = function(type) {
	var me = this;

	switch(type) {
		case "hide":
		
			break;
		case "scroll":
			if (lth6) {
				window.attachEvent("onscroll", function(){
					me.fitMsgBoxPos();
				});
			}
//			try {
//				document.body.attachEvent("onscroll", function(){
//					me.fitMsgBoxPos();
//				});
//			}catch(ex) {
//				document.addEventListener("scroll", function(){
//					me.fitMsgBoxPos();
//				}, true);
//			}
			break;
		default:
			type = "auto";
			break;
	}
	
	this.scrollType = type;
}

FWK.UI.PageMask.prototype.showScroll = function() {	
	if (this.scrollType=="hide") {
		var html = document.getElementsByTagName("html")[0];
		if (html) {
			html.style.overflow="hidden";
			html.style.paddingRight = (document.documentElement.offsetWidth - document.body.offsetWidth) + "px";
		}
	} else if (this.scrollType=="scroll") {
		this.getBox().style.position = lth6?"absolute":"fixed";
	}
}
FWK.UI.PageMask.prototype.hideScroll = function() {
	if (this.scrollType == "hide") {
		var html = document.getElementsByTagName("html")[0];
		if (html) {
			html.style.overflow = "auto";
			html.style.paddingRight = "0";
		}
	} else if (this.scrollType == "scroll") {
		this.getBox().style.position = "absolute";
	}
}

/**
 * 获取遮罩dom引用
 */
FWK.UI.PageMask.prototype.getMask = function() {
	return this.V.mask;
}

/**
 * 获取遮罩内嵌元素dom引用
 */
FWK.UI.PageMask.prototype.getBox = function() {
	return this.V.msgBox;
}
